package liuyang.bigeventserver.modules.web.controller;

import jakarta.validation.constraints.Pattern;
import liuyang.bigeventserver.common.R;
import liuyang.bigeventserver.modules.security.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 实验一些框架行为
 *
 * @author xconf
 * @since 2024/2/29
 */
@RestController
@RequestMapping("jsr303")
@Slf4j
@Validated// 注意要加这个启动验证
public class TestJsr303Controller {

    /**
     * 实测：
     * 可以拦截
     * http://localhost:8080/jsr303/01?username=foo&password=bar
     * 但注意这个可以通过这种形式的验证，要避免出现。
     * http://localhost:8080/jsr303/01?
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("01")
    public R testJsr303(@Pattern(regexp = "^\\w{5,16}$") String username
            , @Pattern(regexp = "^\\w{5,16}$") String password) {
        log.debug("username = {}, password = {}", username, password);
        return R.ok("参数校验通过");
    }

    @PostMapping("02")
    public R add(@RequestBody @Validated(User.Add.class) User user) {
        log.debug("param user = {}", user);
        return R.ok("参数校验通过");
    }

    @PostMapping("03")
    public R update(@RequestBody @Validated(User.Update.class) User user) {
        log.debug("param user = {}", user);
        return R.ok("参数校验通过");
    }

}
